package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;

import com.stark.calculator.mortgage.model.LoanModel;

import java.math.BigDecimal;
import java.math.RoundingMode;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityHouseAllResultBinding;

public class HouseAllResultActivity extends BaseAc<ActivityHouseAllResultBinding> {

    public static LoanModel loanModel = null;

    @Override
    protected int onCreate() {
        return R.layout.activity_house_all_result;
    }

    @Override
    protected void initView() {
        mDataBinding.ivHouseAllResultBack.setOnClickListener(v -> onBackPressed());
        calculateCommercialResult(loanModel.getAmount(), loanModel.getRate(), loanModel.getYearCount());
        calculateFundResult(loanModel.getFundAmount(), loanModel.getFundRate(), loanModel.getYearCount());
    }

    @Override
    protected void initData() {

    }

    @SuppressLint("DefaultLocale")
    protected void calculateCommercialResult(float amount, float rate, int year) {
        // 基础数据
        BigDecimal rateRatio = new BigDecimal(100);
        BigDecimal moneyRatio = new BigDecimal(10000);
        BigDecimal monthRatio = new BigDecimal(12);
        BigDecimal foundRatio = new BigDecimal(1);
        BigDecimal towNumber = new BigDecimal(2);

        // 获取月利率
        BigDecimal yearRateNumber = new BigDecimal(rate);
        yearRateNumber = yearRateNumber.divide(rateRatio);
        BigDecimal monthRateNumber = yearRateNumber.divide(monthRatio, 18, RoundingMode.CEILING);

        // 获取还款月数
        Integer monthCount = year * 12;
        BigDecimal yearCountNumber = new BigDecimal(year);
        BigDecimal monthCountNumber = yearCountNumber.multiply(monthRatio);

        // 贷款总金额
        BigDecimal loanMoneyNumber = new BigDecimal(amount);
        loanMoneyNumber = loanMoneyNumber.multiply(moneyRatio);

        // 等额本息计算公式：〔贷款本金×月利率×（1＋月利率）＾还款月数〕÷〔（1＋月利率）＾还款月数－1〕

        // 1.贷款本金×月利率
        BigDecimal step1Number = loanMoneyNumber.multiply(monthRateNumber);
        // 2.（1＋月利率）
        BigDecimal step2Number = monthRateNumber.add(foundRatio);
        // 3.（1＋月利率）＾还款月数
        BigDecimal step3Number = step2Number.pow(monthCount);
        // 4.〔（1＋月利率）＾还款月数－1〕
        BigDecimal step4Number = step3Number.subtract(foundRatio);

        // 5.〔贷款本金×月利率×（1＋月利率）＾还款月数〕÷〔（1＋月利率）＾还款月数－1〕
        BigDecimal bxMonthPayMoneyNumber = step1Number.multiply(step3Number).divide(step4Number, 18, RoundingMode.CEILING);

        mDataBinding.tvDkze1.setText(String.format("%.2f", amount));
        mDataBinding.tvDkqx1.setText(getString(R.string.term_format, year, monthCountNumber.toBigInteger()));

        double bxMPayMoney = bxMonthPayMoneyNumber.doubleValue();
        double totalPayMoney = bxMPayMoney * monthCount / 10000;
        double totalInterestMoney = totalPayMoney - amount;


        mDataBinding.tvMyhk1.setText(String.format("%.2f", bxMPayMoney));
        mDataBinding.tvZhk1.setText(String.format("%.2f", totalPayMoney));
        mDataBinding.tvLxze1.setText(String.format("%.2f", totalInterestMoney));


        // 等额本金计算公式：每月还款金额 = （贷款本金 / 还款月数）+（本金 — （（贷款本金 / 还款月数 ）* 第几期）×每月利率 。

        // 1.（贷款本金 / 还款月数）
        BigDecimal cap1Number = loanMoneyNumber.divide(monthCountNumber, 18, RoundingMode.CEILING);
        // 3.本金 × 每月利率
        BigDecimal cap3Number = loanMoneyNumber.multiply(monthRateNumber);
        // 首月还款
        BigDecimal capFirstMonthPayMoneyNumber = cap1Number.add(cap3Number);
        // 每月递减
        BigDecimal decreaseMoneyNumber = cap1Number.multiply(monthRateNumber);
        // 还款总利息
        BigDecimal totalInterestNumber = loanMoneyNumber.multiply(monthRateNumber);
        totalInterestNumber = totalInterestNumber.multiply(monthCountNumber.add(foundRatio));
        totalInterestNumber = totalInterestNumber.divide(towNumber, 18, RoundingMode.CEILING);

        double capPerMonthDecMoney = decreaseMoneyNumber.doubleValue();
        double capResultMoney = capFirstMonthPayMoneyNumber.doubleValue();
        double capTotalInterestMoney = totalInterestNumber.doubleValue() / 10000.0;
        double capTotalPayMoney = capTotalInterestMoney + amount;

        mDataBinding.tvSyhk1.setText(String.format("%.2f", capResultMoney));
        mDataBinding.tvMydj1.setText(String.format("%.2f", capPerMonthDecMoney));
        mDataBinding.tvBjZhk1.setText(String.format("%.2f", capTotalPayMoney));
        mDataBinding.tvBjLxze1.setText(String.format("%.2f", capTotalInterestMoney));
    }

    @SuppressLint("DefaultLocale")
    protected void calculateFundResult(float amount, float rate, int year) {
        // 基础数据
        BigDecimal rateRatio = new BigDecimal(100);
        BigDecimal moneyRatio = new BigDecimal(10000);
        BigDecimal monthRatio = new BigDecimal(12);
        BigDecimal foundRatio = new BigDecimal(1);
        BigDecimal towNumber = new BigDecimal(2);

        // 获取月利率
        BigDecimal yearRateNumber = new BigDecimal(rate);
        yearRateNumber = yearRateNumber.divide(rateRatio);
        BigDecimal monthRateNumber = yearRateNumber.divide(monthRatio, 18, RoundingMode.CEILING);

        // 获取还款月数
        Integer monthCount = year * 12;
        BigDecimal yearCountNumber = new BigDecimal(year);
        BigDecimal monthCountNumber = yearCountNumber.multiply(monthRatio);

        // 贷款总金额
        BigDecimal loanMoneyNumber = new BigDecimal(amount);
        loanMoneyNumber = loanMoneyNumber.multiply(moneyRatio);

        // 等额本息计算公式：〔贷款本金×月利率×（1＋月利率）＾还款月数〕÷〔（1＋月利率）＾还款月数－1〕

        // 1.贷款本金×月利率
        BigDecimal step1Number = loanMoneyNumber.multiply(monthRateNumber);
        // 2.（1＋月利率）
        BigDecimal step2Number = monthRateNumber.add(foundRatio);
        // 3.（1＋月利率）＾还款月数
        BigDecimal step3Number = step2Number.pow(monthCount);
        // 4.〔（1＋月利率）＾还款月数－1〕
        BigDecimal step4Number = step3Number.subtract(foundRatio);

        // 5.〔贷款本金×月利率×（1＋月利率）＾还款月数〕÷〔（1＋月利率）＾还款月数－1〕
        BigDecimal bxMonthPayMoneyNumber = step1Number.multiply(step3Number).divide(step4Number, 18, RoundingMode.CEILING);

        mDataBinding.tvDkze2.setText(String.format("%.2f", amount));
        mDataBinding.tvDkqx2.setText(getString(R.string.term_format, year, monthCountNumber.toBigInteger()));

        double bxMPayMoney = bxMonthPayMoneyNumber.doubleValue();
        double totalPayMoney = bxMPayMoney * monthCount / 10000;
        double totalInterestMoney = totalPayMoney - amount;


        mDataBinding.tvMyhk2.setText(String.format("%.2f", bxMPayMoney));
        mDataBinding.tvZhk2.setText(String.format("%.2f", totalPayMoney));
        mDataBinding.tvLxze2.setText(String.format("%.2f", totalInterestMoney));


        // 等额本金计算公式：每月还款金额 = （贷款本金 / 还款月数）+（本金 — （（贷款本金 / 还款月数 ）* 第几期）×每月利率 。

        // 1.（贷款本金 / 还款月数）
        BigDecimal cap1Number = loanMoneyNumber.divide(monthCountNumber, 18, RoundingMode.CEILING);
        // 3.本金 × 每月利率
        BigDecimal cap3Number = loanMoneyNumber.multiply(monthRateNumber);
        // 首月还款
        BigDecimal capFirstMonthPayMoneyNumber = cap1Number.add(cap3Number);
        // 每月递减
        BigDecimal decreaseMoneyNumber = cap1Number.multiply(monthRateNumber);
        // 还款总利息
        BigDecimal totalInterestNumber = loanMoneyNumber.multiply(monthRateNumber);
        totalInterestNumber = totalInterestNumber.multiply(monthCountNumber.add(foundRatio));
        totalInterestNumber = totalInterestNumber.divide(towNumber, 18, RoundingMode.CEILING);

        double capPerMonthDecMoney = decreaseMoneyNumber.doubleValue();
        double capResultMoney = capFirstMonthPayMoneyNumber.doubleValue();
        double capTotalInterestMoney = totalInterestNumber.doubleValue() / 10000.0;
        double capTotalPayMoney = capTotalInterestMoney + amount;

        mDataBinding.tvSyhk2.setText(String.format("%.2f", capResultMoney));
        mDataBinding.tvMydj2.setText(String.format("%.2f", capPerMonthDecMoney));
        mDataBinding.tvBjZhk2.setText(String.format("%.2f", capTotalPayMoney));
        mDataBinding.tvBjLxze2.setText(String.format("%.2f", capTotalInterestMoney));
    }

}